package com.xiaoyan.texteditor.service;

import javax.swing.*;
import java.io.*;

/**
 * ClassName: OpenFileServiceImpl
 * Package: com.xiaoyan.texteditor.service
 * Description:
 *
 * @Author Xiaoyan_Lu
 * @Create 2023/12/3 16:48
 * @Version 1.0
 */
public class OpenFileServiceImpl implements OpenFileService{
    private BufferedReader bufferedReader = null;
    @Override
    public StringBuilder open(File selectedFile, JTextArea jTextArea) {
        //3.若没有，则直接打开
        //3.1读取指定文件中的信息到文本框中
        StringBuilder sb = new StringBuilder();
        try {
            bufferedReader = new BufferedReader(new FileReader(selectedFile.getAbsolutePath()));
            String str = "";
            while ((str = bufferedReader.readLine())!=null){
                sb.append(str+"\n");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return sb;
    }
}
